package com.example.testcamara;

import android.content.Context;
import android.media.MediaPlayer;

import java.io.File;

public class Test2 {

    public static final String AUI_ROOT = "aui_root/";
    public static final String TEMPORAY_ROOT = "temporary/";
    public static final String DOCUMENTS_ROOT = "documents/";
    public static final String EXTERNAL_ROOT = "external/";
    public static final String DEFAULT_FILE_ROOT = "js/";

//    public static String processUriPath(String videoUrl) {
//         MediaPlayer mMediaPlayer;
//        if (videoUrl.startsWith("file://")) {
//            return processUriPath(MyApp.getApplicationContext(), videoUrl.substring("file://".length()));
//        }
//        return videoUrl;
//    }

    /**
     * 解析Uri中的虚拟路径
     * @param context
     * @param ajxPath
     * @return
     */
    public static String processUriPath(Context context, String ajxPath) {
        // 将目录中的"aui_root"替换成auiRoot的目录
        if (ajxPath.startsWith(AUI_ROOT)) {
            ajxPath = getAbsolutePath(getUriAuiRoot(context), ajxPath.substring(AUI_ROOT.length()));
        } else if (ajxPath.startsWith("/" + AUI_ROOT)) {
            // 兼容 file:///aui_root/xxx的case
            ajxPath = getAbsolutePath(getUriAuiRoot(context), ajxPath.substring(AUI_ROOT.length() + 1));
        } else if (ajxPath.startsWith(TEMPORAY_ROOT)) {
            // 针对 file://temporay/xxx的case
            ajxPath = getAbsolutePath(getUriTemporayRoot(context), ajxPath.substring(TEMPORAY_ROOT.length()));
        } else if (ajxPath.startsWith(DOCUMENTS_ROOT)) {
            // 针对 file://documents/xxx的case
            ajxPath = getAbsolutePath(getUriDocumentsRoot(context), ajxPath.substring(DOCUMENTS_ROOT.length()));
        } else if (ajxPath.startsWith(EXTERNAL_ROOT)) {
            // 针对 file://external/xxx的case
            ajxPath = getAbsolutePath(getExternalFilesDir(context), ajxPath.substring(EXTERNAL_ROOT.length()));
        } else {
            ajxPath = null;
        }
        return ajxPath;
    }

    /**
     * 获取文件绝对路径
     *
     * @param dir
     * @param filename
     * @return
     */
    public static String getAbsolutePath(File dir, String filename) {
        return new File(dir, filename).getAbsolutePath();
    }

    /**
     * 获取应用的外部存储目录
     *
     * @param context
     * @return
     */
    public static File getExternalFilesDir(Context context) {
        File f = context.getExternalFilesDir(null);
        //sdcard 不可用时，切换到内置沙箱
        if(f == null) {
            return context.getFilesDir();
        }
        return f;
    }

    /**
     * 获取虚拟路径Aui_Root路径
     *
     * @param context
     * @return
     */
    public static File getUriAuiRoot(Context context) {
        return new File(getExternalFilesDir(context), DEFAULT_FILE_ROOT);
    }

    /**
     * 获取虚拟路径Temporay路径
     *
     * @param context
     * @return
     */
    public static File getUriTemporayRoot(Context context) {
        return getCacheDir(context);
    }

    /**
     * 获取虚拟路径Documents路径
     *
     * @param context
     * @return
     */
    public static File getUriDocumentsRoot(Context context) {
        return getFilesDir(context);
    }

    /**
     * 获取应用内部存储目录
     *
     * @param context
     * @return
     */
    public static File getFilesDir(Context context) {
        return context.getFilesDir();
    }

    /**
     * 获取应用内部缓存目录
     *
     * @param context
     * @return
     */
    public static File getCacheDir(Context context) {
        return context.getCacheDir();
    }
}
